package com.seek.edustatistics.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.seek.edustatistics.client.CourseClient;
import com.seek.edustatistics.client.UcenterClient;
import com.seek.edustatistics.entity.StatisticsDaily;
import com.seek.edustatistics.mapper.StatisticsDailyMapper;
import com.seek.edustatistics.service.StatisticsDailyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author wml
 * @since 2022-03-05
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {

    @Autowired
    private UcenterClient ucenterClient;

    @Autowired
    private CourseClient courseClient;

    // 统计某一天注册人数，生成统计数据
    @Override
    public void countRegister(String date) {
        // 添加记录之前删除表相同日期的数据
        QueryWrapper<StatisticsDaily> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("date_calculated", date);
        baseMapper.delete(queryWrapper);
        // 远程调用得到某一天的注册人数
        Integer count = (Integer) ucenterClient.countRegister(date).getData().get("countRegister");
        // 把获取到的数据添加到数据库中
        StatisticsDaily statisticsDaily = new StatisticsDaily();
        statisticsDaily.setDateCalculated(date); // 统计日期
        statisticsDaily.setRegisterNum(count); // 注册人数
        statisticsDaily.setLoginNum(RandomUtils.nextInt(100, 200)); // 登录人数
        statisticsDaily.setVideoViewNum(RandomUtils.nextInt(100, 200)); // 每日播放视频数
        statisticsDaily.setCourseNum(RandomUtils.nextInt(100, 200)); // 每日新增课程数
        baseMapper.insert(statisticsDaily);
    }

    // 图表显示，返回两部分数据，日期、数量的 json 数组
    @Override
    public Map<String, Object> showData(String type, String begin, String end) {
        QueryWrapper<StatisticsDaily> queryWrapper = new QueryWrapper<>();
        queryWrapper.between("date_calculated", begin, end);
        queryWrapper.select("date_calculated", type);
        List<StatisticsDaily> statisticsDailies = baseMapper.selectList(queryWrapper);
        List<String> dateCalculatedList = new ArrayList<>();
        List<Integer> numList = new ArrayList<>();
        for (StatisticsDaily statisticsDaily : statisticsDailies) {
            dateCalculatedList.add(statisticsDaily.getDateCalculated());
            switch (type) {
                case "login_num": numList.add(statisticsDaily.getLoginNum());break;
                case "register_num": numList.add(statisticsDaily.getRegisterNum());break;
                case "video_view_num": numList.add(statisticsDaily.getVideoViewNum());break;
                case "course_num": numList.add(statisticsDaily.getCourseNum());
            }
        }
        Map<String, Object> map = new HashMap<>();
        map.put("dateCalculatedList", dateCalculatedList);
        map.put("numList", numList);
        return map;
    }

    // 统计一个月以内注册的人数，按天数统计
    @Override
    public Map<String, Object> getRegistryMonth() {
        return ucenterClient.getRegistryMonth().getData();
    }

    @Override
    public Map<String, Object> getRegistryYear() {
        return ucenterClient.getRegistryYear().getData();
    }

    @Override
    public Map<String, Object> getPopularCourse() {
        return courseClient.getPopularCourse().getData();
    }
}
